<template>
  <ex-dialog width="700px" class="ex-diolog" :title="title" :visible="visible" :close-on-click-modal="false"
    @closeDialog="handleClose">
    <div class="main" v-loading="loading">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" :label-width="labelWidth" class="demo-ruleForm">
        <el-form-item label="奖励类型" prop="awardType">
          <el-select v-model="ruleForm.awardType" placeholder="请选择" filterable clearable :style="{width: inputWidth}">
            <el-option v-for="(item,index) in typeList" :key="index" :label="item.value" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="用户手机号" prop="phone">
          <el-input v-model.number="ruleForm.phone" placeholder="请输入" :style="{width: inputWidth}" @blur="blurInput">
          </el-input>
          <span class="real_name_status" v-if="realNameInfo==1"><span class="el-icon-success el-icon"></span>已实名</span>
          <span class="real_name_status" v-if="realNameInfo==3"><span
              class="el-icon-success el-icon-no"></span>未注册</span>
          <span class="real_name_status" v-if="realNameInfo==2"><span
              class="el-icon-success el-icon-no"></span>未实名</span>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model="ruleForm.sort" placeholder="请输入" :style="{width: inputWidth}"></el-input>
        </el-form-item>
        <el-form-item label="获奖金数量" prop="amount">
          <el-input v-model="ruleForm.amount" placeholder="请输入" :style="{width: inputWidth}"></el-input>
          <span style="margin:0 30px 0 10px">{{awardAssetTypeDict}}</span>
          <el-checkbox v-model="checked" :disabled="disabled">虚拟奖金</el-checkbox>
        </el-form-item>
      </el-form>
    </div>
    <div class="button_box">
      <el-button type="primary" :loading="AgreeLoading" @click="submitForm('ruleForm')">确认添加</el-button>
      <el-button @click="handleClose">取消</el-button>
    </div>
  </ex-dialog>
</template>
<script>
  import Loading from "@/components/Loading/index";
  // api
  import {
    addJackpotAwardAssign,
    jackpotAwardRealName,
  } from "@/api/consumeMiningManagement/onlineClockingActivity";

  export default {
    components: {
      Loading,
    },
    props: {
      visible: Boolean,
      id: String,
      awardAssetType: String
    },
    data() {
      return {
        labelWidth: '120px',
        inputWidth: '300px',
        title: '添加指定奖励',
        loading: false,
        disabled: true,
        typeList: [], //奖励类型数组
        checked: true, //是否选择虚拟奖励
        realNameInfo: '', //1 已实名 2 未实名 3 未注册
        awardAssetTypeDict: '', //奖池资产类型名称
        AgreeLoading: false, //按钮loading
        ruleForm: {
          amount: '', //奖励数量
          awardType: '', //奖励类型 1排行 2随机 shenye_punch_jackpot_award_record_type
          isVirtual: '', //是否虚拟奖励 1 是 2 否
          phone: '', //手机号
          punchActivityId: '', //打卡活动id
          sort: '', //排序
        },
        rules: {
          amount: [{
            required: true,
            message: "请输入获奖金数量",
            trigger: ['change', 'bulr'],
          }],
          awardType: [{
            required: true,
            message: '请选择奖励类型',
            trigger: ['change', 'bulr'],
          }],
          phone: [{
            required: true,
            message: '请输入用户手机号',
            trigger: ['change', 'bulr'],
          }],
          sort: [{
            required: true,
            message: '请输入排序',
            trigger: ['change', 'bulr'],
          }],
        },
      };
    },
    computed: {},
    created() {
      this.getDict()
      this.ruleForm.punchActivityId = this.id
      if (this.awardAssetType == 1) {
        this.awardAssetTypeDict = '椰分'
      } else if (this.awardAssetType == 2) {
        this.awardAssetTypeDict = 'CoCo'
      } else {
        this.awardAssetTypeDict = '现金'
      }
    },
    methods: {
      //获取类型字典
      getDict() {
        var dictType = "shenye_punch_jackpot_award_record_type";
        this.$getSelectList(dictType)
          .then((res) => {
            this.typeList = res.result[0].dictList;
          })
          .finally(() => {
            this.loading = false;
          });
      },
      //取消
      handleClose(formName) {
        this.$emit("update:visible", false);
      },
      blurInput(e) { //用户手机号输入框失去焦点时查询用户实名类型  1 已实名 2 未实名 3 未注册
        //   console.log(this.ruleForm.phone)
        if (this.ruleForm.phone != '') {
          var params = {
            phone: this.ruleForm.phone
          };
          jackpotAwardRealName(params).then((res) => {
            this.realNameInfo = res.result
            if (res.result == 1) {
              this.disabled = false
            } else {
              this.disabled = true
            }
          })
        } else {
          this.realNameInfo = ''
        }

      },
      submitForm(formName) {
        //创建确定
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.AgreeLoading = true
            if (this.checked) {
              this.ruleForm.isVirtual = 1
            } else {
              this.ruleForm.isVirtual = 2
            }
            var params = {
              ...this.ruleForm,
            };
            addJackpotAwardAssign(params).then((res) => {
                this.$message({
                  message: '添加成功',
                  type: "success",
                });
                setTimeout(() => {
                  this.$emit('success')
                  this.handleClose()
                  this.AgreeLoading = false
                }, 1000);
              })
              .catch(() => {
                this.AgreeLoading = false;
              });

          } else {
            console.log("error submit!!");
            return false;
          }
        });
      },
    },
  };

</script>
<style lang="scss" scoped>
  .ex-diolog {
    ::v-deep .ex--dialog--footer {
      border: none !important;
      padding: 0 !important;
    }

    ::v-deep .ex--dialog--main {
      padding: 10px 20px !important;
    }
  }

  .main {
    padding: 20px 20px;
  }

  .button_box {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px 0;
  }

  .el-icon {
    font-size: 16px;
    color: #05ce69;
    margin: 0 4px 0;
  }

  .el-icon-no {
    font-size: 16px;
    color: #ee3110;
    margin: 0 4px 0;
  }

</style>
